<template>
  <div class="fog-prc">
    <!-- threejs官网的 ‘一些有用的链接’-fog -->
    <div class="canvas" ref="canvas"></div>
  </div>
</template>

<script lang="ts">
import { Vue, Options } from 'vue-property-decorator';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const res = import.meta.globEager('../../assets/*.png');
const scene =  new THREE.Scene();
// Fog使用更普遍 因为它支持设定影响区域
scene.fog = new THREE.Fog('blue', 1, 100);
scene.background = new THREE.Color('red');
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(10, 8, 20);
camera.lookAt(0, 0, 0);
{
  const light = new THREE.DirectionalLight(0xffffff, 1);
  light.position.set(1, 2, 4);
  scene.add(light);
}
const renderer = new THREE.WebGLRenderer({
  alpha: true,
  antialias: true,
});
renderer.debug.checkShaderErrors = false;
renderer.setClearColor(new THREE.Color(), 0);
renderer.setPixelRatio(750 / window.innerWidth);
@Options({ components: {} })
export default class FogPrc extends Vue {
  public rect: DOMRect|null = null;

  public mounted() {
    this.rect = (this.$el as HTMLElement).getBoundingClientRect();
    renderer.setSize(this.rect.width, this.rect.height, false);
    (this.$refs.canvas as HTMLDivElement).append(renderer.domElement);

    const geometry = new THREE.BoxGeometry(5, 10, 5);
    const material = new THREE.MeshLambertMaterial({ color: 'white' });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    renderer.setAnimationLoop(() => {
      // mesh.rotation.x += 0.01;
      // mesh.rotation.y += 0.01;
      mesh.rotation.z += 0.01;
      renderer.render(scene, camera);
    });

    const controls = new OrbitControls(camera, renderer.domElement);
    controls.enableZoom = false;
  }
}
</script>

<style lang="postcss" scoped>
  .fog-prc{
    touch-action: none;
    position: absolute;
    left: 0;top: 0;
    width: 100%;height: 100%;
    background: #98bbd1;
    .canvas{
      position: absolute;
      left: 0;top: 0;
      width: 100%;height: 100%;
       :deep(canvas){
        width: 100%;height: 100%;
      }
    }
  }
</style>
